package com.redis;

import com.redis.test02.MyRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 作者: LCG
 * 日期: 2019/9/10 16:55
 * 描述:
 */
@RestController
public class Test02Controller {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping("addId")
    public String addId(){

        String key = UUID.randomUUID().toString();

        redisTemplate.opsForValue().set(key,key);

        return key;
    }


    /**
     * 接口的幂等性调用
     * @return
     */
    @RequestMapping("test100/{id}")
    public String testMeDengXing(@PathVariable("id") String id){

        boolean id1 = MyRedisUtil.findId(id, id, redisTemplate);

        if(id1){
            System.out.println("======执行自己的业务=====");
            return "ok";
        }else{
            return "已经操作过，请勿重复操作！";
        }
    }

    @RequestMapping("test101")
    public String test101(){

        boolean lock = MyRedisUtil.getLock("zhangsan", "zhangsan", redisTemplate, "10");

        System.out.println(lock);

        return "ok->"+lock;

    }

    @RequestMapping("test102")
    public String test102(){

        boolean lock = MyRedisUtil.unLock("zhangsan","zhangsan",redisTemplate);

        return "ok->"+lock;

    }


}
